package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.Bindable;
import android.net.Uri;
import android.view.View;

import com.solidfire.gson.internal.LinkedTreeMap;

import java.util.Timer;
import java.util.TimerTask;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.LoadingDialog;
import chuangyi.com.org.DOMIHome.PopupActivity;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.OSSUtil;
import chuangyi.com.org.DOMIHome.Utils.Utils;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.WaitActivityResult;

/**
 * Created by zxh on 16/12/14.
 */

public class WithdrawModel extends ObservableWithGoBackHandler implements WaitActivityResult {
    private double balance;
    private double sxf;
    private String withdrawAmount;
    private String bank;
    private String cardNo;
    private String name;
    private String image;
    private String code;
    private String sendMessage;
    private boolean allowSend;

    @Bindable
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
        notifyPropertyChanged(BR.code);
    }

    @Bindable
    public String getSendMessage() {
        return sendMessage;
    }

    public void setSendMessage(String sendMessage) {
        this.sendMessage = sendMessage;
        notifyPropertyChanged(BR.sendMessage);
    }

    @Bindable
    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
        notifyPropertyChanged(BR.balance);
    }

    @Bindable
    public double getSxf() {
        return sxf;
    }

    @Bindable
    public String getWithdrawAmount() {
        return withdrawAmount;
    }

    public void setWithdrawAmount(String withdrawAmount) {
        this.withdrawAmount = withdrawAmount;
        notifyPropertyChanged(BR.incomeAmount);
        notifyPropertyChanged(BR.withdrawAmount);
    }

    @Bindable
    public double getIncomeAmount() {
        try {
            double value = Double.valueOf(withdrawAmount);
            return value * (1 - sxf);
        } catch (Exception ex) {
            return 0;
        }
    }

    @Bindable
    public String getBank() {
        return bank;
    }

    public void setBank(String bank) {
        this.bank = bank;
        notifyPropertyChanged(BR.bank);
    }

    @Bindable
    public String getCardNo() {
        return cardNo;
    }

    public void setCardNo(String cardNo) {
        this.cardNo = cardNo;
        notifyPropertyChanged(BR.cardNo);
    }

    @Bindable
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        notifyPropertyChanged(BR.name);
    }

    @Bindable
    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
        notifyPropertyChanged(BR.image);
    }

    public WithdrawModel(Context context) {
        super(context);
        super.getHandler().setTitle("提现");
        this.load();
        allowSend = true;
        this.sendMessage = "获取验证码";
    }

    public void load() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                LinkedTreeMap map = WebApi.goTixian(getContext());
                if (map != null) {
                    setBalance(Utils.getDoubleValue(map.get("balance")));
                    sxf = Utils.getDoubleValue(map.get("sxf"));
                    notifyPropertyChanged(BR.sxf);
                    notifyPropertyChanged(BR.incomeAmount);
                }
            }
        }).start();
    }

    /**
     * 点击提现
     *
     * @param view
     */
    public void onClickWithdraw(View view) {
        if (Utils.isNullOrEmpty(getCode())) {
            showToast("请输入验证码");
            return;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    double amount = Double.valueOf(getWithdrawAmount());
                    if (WebApi.doTixian(getContext(), amount, getIncomeAmount(), getCardNo(), getBank(),
                            getName(), User.getCurrentUser().getPhone(), getCode())) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                showToast("提现申请已提交");
                                HomeActivity.popFragment();
                            }
                        });
                    }
                } catch (Exception ex) {
                    showToast("提交数据时发生错误");
                }
            }
        }).start();
    }

    /**
     * 点击选择照片
     *
     * @param view
     */
    public void onClickSelectPhoto(View view) {
        SelectPhotoModel model = new SelectPhotoModel(getContext());
        PopupActivity.startActivityForResult((Activity) getContext(),
                R.layout.v_dialog_modify_photo, HomeActivity.REQUEST_IMAGE, BR.photoSelector, model);
    }

    public void onClickSendSMS(View view) {
        if (allowSend) {
            final Context context = super.getContext();
            final Timer timer = new Timer();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    if (WebApi.SendSMS(context, User.getCurrentUser().getPhone())) {
                        allowSend = false;
                        TimerTask task = new TimerTask() {
                            int count = 60;

                            @Override
                            public void run() {
                                if (count > 0) {
                                    setSendMessage(String.valueOf(count) + "秒后重发");
                                } else {
                                    allowSend = true;
                                    timer.cancel();
                                    timer.purge();
                                    setSendMessage("重新获取");
                                }
                                count--;
                            }
                        };
                        timer.schedule(task, 0, 1000);
                    }
                }
            }).start();
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == HomeActivity.REQUEST_IMAGE) {
                final Uri uri = data.getData();
                LoadingDialog.getInstance(getContext()).show();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Context context = getContext();
                        String url = OSSUtil.uploadFile(context.getApplicationContext(), Utils.getPathByUri(uri, context));
                        setImage(url);
                        LoadingDialog.getInstance(getContext()).dismiss();
                    }
                }).start();
            }
        }
    }
}
